import { defineStore } from 'pinia'
import { Names } from './store-name'

type User = {
    name:string,
    age:number
}

let result:User = {
    name: '你好',
    age: 99,
}

const Login = ():Promise<User> => new Promise((resolve) => {
    setTimeout(() => {
        resolve({
            name: '哈哈哈',
            age: 89898989,
        })
    }, 2000)
})

export const userInfoStore = defineStore(Names.TEST, {
    state: () => ({
        current: 10000,
        name: '小虎',
        user: <User>{},
    }),
    // 计算属性
    getters: {
        newName():string {
            return `$iiii-${this.name}---${this.getUserAge}`
        },
        getUserAge():number {
            return this.user.age || 0
        },
    },
    // method 同步异步操作 提交state
    actions: {
        setCurrent(cur:number) {
            this.current = cur
        },
        async setUser() {
            this.user = await Login()
            this.setName('大大大大大')
        },
        setName(name:string) {
            this.name = name
        },
    },
})

// base
export const userStore = defineStore(Names.BASE, {
    state: () => ({
        current: 10000,
        name: '小虎',
        user: <User>{},
    }),
    // 计算属性
    getters: {
        newName():string {
            return `$iiii-${this.name}---${this.getUserAge}`
        },
        getUserAge():number {
            return this.user.age || 0
        },
    },
    // method 同步异步操作 提交state
    actions: {
        setCurrent(cur:number) {
            this.current = cur
        },
        async setUser() {
            this.user = await Login()
            this.setName('大大大大大')
        },
        setName(name:string) {
            this.name = name
        },
    },
})